<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Special\Controller;
use \Admin\Controller\InitController;
/* 专题后台管理 */
class SpecialController extends InitController {
	public function _initialize() {
		parent::_initialize();
		$this->db = D('Special');
		$this->service = D('Special', 'Service');
	}

	public function init() {
		$page = max(1, (int) I('page'));
		$count = $this->db->count();
		$infos = $this->db->page($page, 6)->order('`listorder` DESC, `createtime` DESC')->select();
		$pages = page($count, 6);
		include $this->admin_tpl('special_list');
	}

	/* 添加专题 */
	public function add() {
		if (submitcheck('dosubmit')) {
			$special = $_POST['special'];
			$id = $this->db->update($special);
			if (!$id) {
				$this->error($this->db->getError());
			}
			$url = U('Special/Index/Show', array('id' => $id));
			$this->db->update(array('id' => $id, 'url' => $url), FALSE);
			$this->success(L('add_special_success'));
		} else {
			$form = new \Common\Library\form();
            /* 加载可用风格 */
            $template_list = template_list();
            foreach ($template_list as $k=>$v) {
                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
                unset($template_list[$k]);
            }
			include $this->admin_tpl('special_add');
		}
	}

	/* 专题编辑 */
	public function edit($specialid = 0) {
		$specialid = (int) $specialid;
		$info = $this->db->getById($specialid);
		if (!$info) $this->error(L('special_not_exist'));
		if (submitcheck('dosubmit')) {
			$special = $_POST['special'];
			$special['id'] = $specialid;
			$special['url'] = U('Special/Index/Show', array('id' => $specialid));
			$id = $this->db->update($special);
			if (!$id) {
				$this->error($this->db->getError());
			}
			$this->success(L('edit_special_success'));			
		} else {
			$form = new \Common\Library\form();
            /* 加载可用风格 */
            $template_list = template_list();
            foreach ($template_list as $k=>$v) {
                $template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
                unset($template_list[$k]);
            }
            include $this->admin_tpl('special_edit');
		}	
	}

	/* 专题删除 */
	public function delete() {
		$ids = (array) I('param.id');
		if ($ids) {
			foreach ($ids as $id) {
				$this->service->_delete_special($id);
			}
			$this->success('操作成功');
		} else {
			$this->error('参数错误');
		}
	}

	/* 专题排序 */
	public function public_listorder() {
		$listorders	= (array) $_POST['listorder'];
		if($listorders) {
			foreach ($listorders as $id => $listorder) {
				if(!is_numeric($id) || !is_numeric($listorder)) continue;
				$this->db->where(array('id' => $id))->setField('listorder', $listorder);
			}
			$this->success(L('update_special_success'));
		} else {
			$this->error('参数错误');
		}
	}

	/* 导入信息 */
	public function import() {
		$catid = I('catid', 0, 'intval');
		$modelid = I('modelid', 0, 'intval');
		$specialid = I('specialid', 0, 'intval');		
		if (submitcheck('dosubmit')) {
			$ids = (array) $_POST['ids'];
			if ($ids) {
				foreach ($ids as $id) {
					$this->service->_import($specialid, $modelid, $id);
				}
			}
			$this->success('操作成功', 'javascript:close_dialog();');
		} else {
			$form = new \Common\Library\form();
			$models = getcache('model','commons');
			$model_datas = array();
			foreach($models as $_k=>$_v) {
				if($_v['module'] != 'document') continue;
				$model_datas[$_v['modelid']] = $_v['name'];
			}
			$model_form = $form::select($model_datas, $modelid, 'name="modelid" onchange="select_categorys(this.value)"', L('select_model'));
			/* 获取内容 */
			$page = max(1, I('page', 0, 'intval'));
			$data = $this->service->_get_import_data($modelid, $where, $page);
			$pages = $this->service->pages;
			include $this->admin_tpl('import_content');
		}
	}

	/* 专题推荐 */
	public function elite() {
		$id = I('id', 0, 'intval');
		if($id < 1) $this->error('参数错误');
		$elite = $this->db->getFieldById($id, 'elite');
		$elite = (empty($elite)) ? 1 : 0;
		$this->db->where(array('id' => $id))->setField('elite', $elite);
		$this->success('操作成功');
	}

	/**
	 * 按模型ID列出模型下的栏目
	 */
	public function public_categorys_list() {
		$catid = I('catid', 0, 'intval');
		$modelid = I('modelid', 0, 'intval');
		if(!isset($modelid) || empty($modelid)) exit('');
		$form = new \Common\Library\form();
		exit($form::select_category($catid, 'name="catid" id="catid"', L('please_select'), $modelid, 0, 1));
	}

	/* 专题重复检测 */
	public function public_check_special($clientid = 'title') {
		if ($clientid == 'title') {
			$title = trim(I('title'));
			if(!empty($title)) {
				$sql = array('title' => $title);
				$id = (int) I('id');
				if ($id > 1) $sql['id'] = array("NEQ", $id);
				if ($this->db->where($sql)->count()) {
					$this->error(L('special_exist'));
				}
			}
		}
		$this->success('专题可用');
	}
}